package shopping.web.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import shopping.auth.Auth;
import shopping.domain.User;
import shopping.utils.RRHolder;
import shopping.web.vo.ResultVo;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {
    public void success() throws IOException {
        success(null);
    }
    public void success(Object data) throws IOException {
        ResultVo vo = new ResultVo(ResultVo.CODE_SUCCESS,data,"");
        String s = new ObjectMapper().writeValueAsString(vo);
        RRHolder.getResponse().getWriter().write(s);
    }
    public void fail() throws IOException {
        fail(null);
    }
    public void fail(Object data) throws IOException {
        ResultVo vo = new ResultVo(ResultVo.CODE_FAILED,data,"");
        String s = new ObjectMapper().writeValueAsString(vo);
        RRHolder.getResponse().getWriter().write(s);
    }
    public void authFail() throws IOException {
        ResultVo vo = new ResultVo(ResultVo.CODE_AUTH_FAIL,"");
        String s = new ObjectMapper().writeValueAsString(vo);
        RRHolder.getResponse().getWriter().write(s);
    }
    public void noLogin() throws IOException {
        noLogin(null);
    }
    public void noLogin(Object data) throws IOException {
        ResultVo vo = new ResultVo(ResultVo.CODE_NOLOGIN,data,"");
        String s = new ObjectMapper().writeValueAsString(vo);
        RRHolder.getResponse().getWriter().write(s);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String methodName = request.getParameter("md");

        Class clz = this.getClass();

        try {
            Method method1 = clz.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            //执行方法之前判断该方法是否存在@Auth注解
            if(method1.isAnnotationPresent(Auth.class)){
                //存在，就需要登录
                User user = (User) request.getSession().getAttribute("user");
                if(user == null){
                    noLogin();
                    return;
                }
                //代码走到这里，说明方法是需要登录的，并且已经登录
                //获取注解对象的值
                Auth auth = method1.getAnnotation(Auth.class);
                String value = auth.value();
                //user.getRemark() 表示该用户的身份
                if(!value.equals(user.getRemark())){
                    //如果不是该用户的身份，登录失败
                    authFail();
                    return;
                }
            }

            //执行方法
            method1.setAccessible(true);
            method1.invoke(this,request,response);
        } catch (Exception e) {
            e.printStackTrace();

            ResultVo vo = new ResultVo(ResultVo.CODE_SERVER_ERROR,"不好意思，服务器正在维护，请稍后");
            String s = new ObjectMapper().writeValueAsString(vo);
            RRHolder.getResponse().getWriter().write(s);
        }
    }
}
